package edu.cs.usask.ca.silver.businesslogic.ids;

import java.util.Date;
import java.util.UUID;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.commands.generic.Scenario;
import edu.cs.usask.ca.silver.exceptions.SilverBusinessLogicErrorException;

/**
 * 
 * @author Yudi Xue
 */
public class ScenarioID extends BaseId {

	public ScenarioID(UUID value) throws SilverBusinessLogicErrorException {
		super(value);
	}

	// return example: s1, s20
	public String toString() {
		return "s" + uIdVal.toString();
	}

	public UUID getUUIdVal() {
		return uIdVal;
	}

	@Override
	public boolean equals(BaseId id) {
		return ((id.getClass() == this.getClass()) && (((ScenarioID) id)
				.getUUIdVal().compareTo(this.getUUIdVal()) == 0));
	}

	@Override
	public String getEntryNameFromDatabase(StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		String name = borrowedObj.getName(this, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return name;
	}

	@Override
	public BaseId[] getChildrenObjectIds(StorageI storage) throws Exception {
		return null;
	}

	@Override
	public boolean isEntity() {
		return true;
	}

	@Override
	public BaseId getParentObjectId(StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		ScenarioCollectionID scId = borrowedObj.getScenarioCollectionId(this, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return scId;
	}

	@Override
	public boolean hasChildren() {
		return false;
	}

	public void accept(SilverIDVisitor v) throws Exception {
		v.visit(this);
	}

	@Override
	public String getCreatorNameFromDatabase(StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		String name = borrowedObj.getCreator(this, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return name;
	}

	@Override
	public boolean setEntryNameToDatabase(String updateName, StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		boolean result = borrowedObj.setName(this, updateName, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public String getTypeFromDatabase(StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		String result = borrowedObj.getType(this, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public String getDescriptionFromDatabase(StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		String result = borrowedObj.getDescription(this, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean setDescriptionToDatabase(String desc, StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		boolean result = borrowedObj.setDescription(this, desc, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public Date getCreationTime(StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		Date result = borrowedObj.getCreationTime(this, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	public String getParameterComment(ParameterID pId, StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		String result = borrowedObj.getParameterComment(this, pId, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	public String getParameterValue(ParameterID pId, StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		String result = borrowedObj.getParameterValue(this, pId, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

	@Override
	public boolean deleteFromDatabase(StorageI storage) throws Exception {
		Scenario borrowedObj = ((Scenario) Scenario.getObjectPoolFactory()
				.borrowObject());
		boolean result = borrowedObj.deleteScenario(this, storage);
		Scenario.getObjectPoolFactory().returnObject(borrowedObj);
		return result;
	}

}
